// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-20 20:26:34
 * @LastEditors: qiulin ql1746365080@gmail.com
 * @LastEditTime: 2024-12-12 15:14:30
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Material\MAT2Parser.cpp
 */

#include "Import/BDFImport/BulkData/Material/MAT2Parser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Material/IAnisotropic2DMaterialService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (MAT2);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/MAT2/MAT2.md>
*/
// 以下需要修改
enum MAT2key : std::uint8_t
{
    MAT2KEY_MID   = 2,
    MAT2KEY_G11   = 3,
    MAT2KEY_G12   = 4,
    MAT2KEY_G13   = 5,
    MAT2KEY_G22   = 6,
    MAT2KEY_G23   = 7,
    MAT2KEY_G33   = 8,
    MAT2KEY_RHO   = 9,
    MAT2KEY_A1    = 12,
    MAT2KEY_A2    = 13,
    MAT2KEY_A3    = 14,
    MAT2KEY_TREF  = 15,
    MAT2KEY_GE    = 16,
    MAT2KEY_ST    = 17,
    MAT2KEY_SC    = 18,
    MAT2KEY_SS    = 19,
    MAT2KEY_MCSID = 22,
    MAT2KEY_GE11  = 23,
    MAT2KEY_GE12  = 24,
    MAT2KEY_GE13  = 25,
    MAT2KEY_GE22  = 26,
    MAT2KEY_GE23  = 27,
    MAT2KEY_GE33  = 28,
};

Common::Status BDF::BULKDATA::ParseMAT2 (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                         std::vector<std::string>&                               dividedEntryInfo)
{
    // 创建一个用于保存MAT2材料数据的临时结构体对象
    FEM::Anisotropic2DMaterial tempMAT2;

    // TODO(bug 001) 填写m_domainId的正确值
    // 目前m_domainId硬编码为1，需要进一步确认是否正确
    tempMAT2.m_domainId = 1;

    // 初始化当前处理的字符串条目的ID
    int curIDKey = 1;

    // 遍历所有分割后的条目信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 判断当前条目是否为空（即是否全为空格）
        bool const isEmpty = is_all_space (curStrKey);

        // 如果当前条目为空，则记录对应的缺失项
        if (isEmpty)
        {
            curIDKey++;
            continue;
        }

                // 根据条目ID，将解析后的数值存入对应的MAT2字段
        switch (curIDKey)
        {
        case MAT2KEY_MID:
            tempMAT2.m_id = convertToI64 (curStrKey);
            break;
        case MAT2KEY_G11:
            tempMAT2.m_G[0] = convertToReal (curStrKey);
            break;
        case MAT2KEY_G12:
            tempMAT2.m_G[1] = convertToReal (curStrKey);
            break;
        case MAT2KEY_G13:
            tempMAT2.m_G[2] = convertToReal (curStrKey);
            break;
        case MAT2KEY_G22:
            tempMAT2.m_G[3] = convertToReal (curStrKey);
            break;
        case MAT2KEY_G23:
            tempMAT2.m_G[4] = convertToReal (curStrKey);
            break;
        case MAT2KEY_G33:
            tempMAT2.m_G[5] = convertToReal (curStrKey);
            break;
        case MAT2KEY_RHO:
            tempMAT2.m_rho = convertToReal (curStrKey);
            break;
        case MAT2KEY_A1:
            tempMAT2.m_Ai[0] = convertToReal (curStrKey);
            break;
        case MAT2KEY_A2:
            tempMAT2.m_Ai[1] = convertToReal (curStrKey);
            break;
        case MAT2KEY_A3:
            tempMAT2.m_Ai[2] = convertToReal (curStrKey);
            break;
        case MAT2KEY_TREF:
            tempMAT2.m_tref = convertToReal (curStrKey);
            break;
        case MAT2KEY_GE:
            tempMAT2.m_ge = convertToReal (curStrKey);
            break;
        case MAT2KEY_ST:
            tempMAT2.m_ST = convertToReal (curStrKey);
            break;
        case MAT2KEY_SC:
            tempMAT2.m_SC = convertToReal (curStrKey);
            break;
        case MAT2KEY_SS:
            tempMAT2.m_SS = convertToReal (curStrKey);
            break;
        case MAT2KEY_MCSID:
            tempMAT2.m_G[4] = convertToReal (curStrKey);
            break;
        case MAT2KEY_GE11:
        case MAT2KEY_GE12:
        case MAT2KEY_GE13:
        case MAT2KEY_GE22:
        case MAT2KEY_GE23:
        case MAT2KEY_GE33:
        default:
            break;
        }
        curIDKey++;
    }

    // 将解析后的MAT2数据通过数据库服务工厂的IsotropicMaterial服务添加到数据库中
    return dbServiceFactory->get<IAnisotropic2DMaterialService> ()->Add (tempMAT2);
}